package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.penpal.bean.PenpalNoticeResponse;
import com.rongshu.view.penpal.view.NoticeAdapterListener;
import com.widget.SwipeMenuView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：笔友会公告列表
 */
public class PenpalNoticeAdapter extends RecyclerView.Adapter<PenpalNoticeAdapter.ViewHolder> {
    private Context context;
    private List<PenpalNoticeResponse.ResponseBean> list;
    private NoticeAdapterListener listener;

    public PenpalNoticeAdapter(Context context, List<PenpalNoticeResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    public void setAdapterListener(NoticeAdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_notice_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        try {
            viewHolder.content.setText(URLDecoder.decode(list.get(i).getContents(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        viewHolder.time.setText(DateUtils.ConvertDate(list.get(i).getCreate_datetime()));
        if (list.get(i).getIsIstop()) {
            viewHolder.title.setText("当前公告");
            viewHolder.state.setText("取消公告");
            viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.color_f99704));
            viewHolder.line.setVisibility(View.VISIBLE);
            viewHolder.title.setVisibility(View.VISIBLE);
        } else {
            viewHolder.title.setVisibility(View.GONE);
            viewHolder.state.setText("设为新公告");
            viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.color_3CC051));
            viewHolder.line.setVisibility(View.GONE);
        }
        viewHolder.mainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onItemListener(list.get(i));
                }
            }
        });
        viewHolder.state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onSetTopListener(list.get(i));
                }
            }
        });
        viewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.menuView.quickClose();
                if (listener != null) {
                    listener.onDeleteListener(list.get(i));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title, content, time, state;
        private View line;
        private TextView delete;
        private SwipeMenuView menuView;
        private RelativeLayout mainLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.notice_title);
            content = itemView.findViewById(R.id.notice_content);
            time = itemView.findViewById(R.id.notice_time);
            state = itemView.findViewById(R.id.notice_state);
            line = itemView.findViewById(R.id.line);
            delete = itemView.findViewById(R.id.btn_delete);
            menuView = itemView.findViewById(R.id.swipemenu_view);
            mainLayout = itemView.findViewById(R.id.main_layout);
        }
    }
}
